package com.qianfeng.qlm.handforireader.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.activity.NewChapterActivity;
import com.qianfeng.qlm.handforireader.adapter.CategoryDetailAdapter;
import com.qianfeng.qlm.handforireader.model.CategoryDetail;
import com.qianfeng.qlm.handforireader.util.UrlMaker;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/23
 * Email:tadhg299@163.com
 */
public class HotFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener {

    private PullToRefreshListView pullToRefreshListView;

    private CategoryDetailAdapter mAdapter;
    private ArrayList<CategoryDetail> mData;
    private RequestQueue mQueue;
    private int pagenum = 0;

    public HotFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot, container, false);

        mQueue = Volley.newRequestQueue(getActivity());

        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.fragment_hot_pullToRefresh);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        pullToRefreshListView.setOnRefreshListener(this);
        ListView listView = pullToRefreshListView.getRefreshableView();
        mData = new ArrayList<>();
        mAdapter = new CategoryDetailAdapter(getActivity(), mData,mQueue);
        listView.setAdapter(mAdapter);
        getDetail(pagenum);
        listView.setOnItemClickListener(this);

        return view;
    }

    public void getDetail(final int pagenum){
        String url = UrlMaker.GetHotComicList("" + pagenum);
        JsonObjectRequest jsonRequest = new JsonObjectRequest(
                Request.Method.GET,
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("hot_comic_arr");
                            String json = jsonArray.toString();
                            Gson gson = new Gson();
                            List<CategoryDetail> list = new ArrayList<>();
                            list = Arrays.asList(gson.fromJson(json, CategoryDetail[].class));
                            if (mData != null && mData.size() > 0 && pagenum == 0){
                                mData.clear();
                            }
                            mData.addAll(list);
                            mAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("HotFragment","error");
                    }
                }
        );
        mQueue.add(jsonRequest);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(getActivity(),"" + position,Toast.LENGTH_SHORT).show();

        CategoryDetail categoryDetail = mData.get(position-1);
        String url = UrlMaker.GetComicInfor(categoryDetail.getId());

        Log.d("url",url);

        Intent intent = new Intent(getActivity(),NewChapterActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("name",mData.get(position-1).getComic_name());
        startActivity(intent);

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pagenum += 20;
        getDetail(pagenum);
        pullToRefreshListView.onRefreshComplete();
    }
}
